package club.emergency.wechat.common.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.type.CollectionType;

import java.io.IOException;
import java.util.List;
import java.util.Set;

/**
 * Create with IntelliJ IDEA
 * Project Name: business
 * Package Name: club.emergency.wechat_api.common.utils
 * Date: 2018/11/29
 *
 * @author: 快乐的小菌菌
 * Description: Json 转换工具类
 */
public class JsonConvert<T> {

    /**
     * 对象转json
     *
     * @param t 对象
     * @return json串
     */
    public String jsonEncode(T t) {
        String s = null;
        try {
            s = new ObjectMapper().writeValueAsString(t);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return s;
    }

    /**
     * json串转对象
     *
     * @param jsonStr json串
     * @param clazz   泛型对象
     * @return 对象
     */
    public T jsonDecode(String jsonStr, Class<T> clazz) {
        T t = null;
        try {
            t = new ObjectMapper().readValue(jsonStr, clazz);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return t;
    }

    /**
     * json串转对象List
     *
     * @param jsonStr json串
     * @param clazz   泛型对象
     * @return 对象
     */
    public List<T> jsonListDecode(String jsonStr, Class<T> clazz) {
        List<T> tList = null;
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            CollectionType collectionType = objectMapper.getTypeFactory().constructCollectionType(List.class, clazz);
            tList = objectMapper.readValue(jsonStr, collectionType);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return tList;
    }

    /**
     * json串转对象Set
     *
     * @param jsonStr json串
     * @param clazz   泛型对象
     * @return 对象
     */
    public Set<T> jsonSetDecode(String jsonStr, Class<T> clazz) {
        Set<T> tSet = null;
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            CollectionType collectionType = objectMapper.getTypeFactory().constructCollectionType(Set.class, clazz);
            tSet = objectMapper.readValue(jsonStr, collectionType);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 下划线转驼峰，Object转json
     *
     * @param obj 对象
     */
    public String underscoresToCamelEncode(Object obj) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
        return objectMapper.writeValueAsString(obj);
    }

    /**
     * 下划线转驼峰，json转Object
     *
     * @param jsonStr json
     * @param clazz   目标对象
     * @return 泛型对象
     */
    public T underscoresToCamelDecode(String jsonStr, Class<T> clazz) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
        return objectMapper.readValue(jsonStr, clazz);
    }
}
